package com.lincoln.bean;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lincoln on 2017/5/5.
 */
public class ApiCode {
    public static Map<Integer, ApiCode> map = new HashMap<>();
    //请求成功
    public static int SUCCESS = 1;
    //请求失败
    public static int FAIL = -1;

    //参数不足10001
    public static int PARAME_NEED = 10001;
    //CHECK_SUM校验错误10002
    public static int CHECK_SUM_ERROR = 10002;
    //请求其他服务出错10003
    public static int REQUEST_OTHER_ERROR = 10003;
    //权限不足
    public static int NOPERMISSION = 10004;
    //需要登录
    public static int NEED_LOGIN = 10005;
    //用户名不能为空
    public static int USER_NAME_NEED = 10006;
    //密码不能为空
    public static int PASSWORD_NEED = 10007;
    //需要选择等级
    public static int ROLE_ID_NEED = 10008;
    //用户名已存在
    public static int USER_NAME_HAVEN = 10009;
    //对象不存在
    public static int ENTITY_NONE = 10010;
    //等级不存在
    public static int ROLE_NONE = 10011;
    //用户不存在
    public static int USER_NONE = 10012;
    //权限不存在
    public static int PERMISSION_NONE = 10013;
    //图片上传失败
    public static int PIC_UPLOAD_FAIL = 10014;

    static {
        map.put(SUCCESS, new ApiCode(SUCCESS, "请求成功"));
        map.put(FAIL, new ApiCode(FAIL, "请求失败"));
        map.put(PARAME_NEED, new ApiCode(PARAME_NEED, "参数不足"));
        map.put(CHECK_SUM_ERROR, new ApiCode(CHECK_SUM_ERROR, "CHECK_SUM校验错误"));
        map.put(REQUEST_OTHER_ERROR, new ApiCode(REQUEST_OTHER_ERROR, "请求其他服务出错"));
        map.put(NOPERMISSION, new ApiCode(NOPERMISSION, "权限不足"));
        map.put(NEED_LOGIN, new ApiCode(NEED_LOGIN, "需要登录"));
        map.put(USER_NAME_NEED, new ApiCode(USER_NAME_NEED, "用户名不能为空"));
        map.put(PASSWORD_NEED, new ApiCode(PASSWORD_NEED, "密码不能为空"));
        map.put(ROLE_ID_NEED, new ApiCode(ROLE_ID_NEED, "需要选择等级"));
        map.put(USER_NAME_HAVEN, new ApiCode(USER_NAME_HAVEN, "用户名已存在"));
        map.put(ENTITY_NONE, new ApiCode(ENTITY_NONE, "对象不存在"));
        map.put(ROLE_NONE, new ApiCode(ROLE_NONE, "等级不存在"));
        map.put(USER_NONE, new ApiCode(USER_NONE, "用户不存在"));
        map.put(PERMISSION_NONE, new ApiCode(PERMISSION_NONE, "权限不存在"));
        map.put(PIC_UPLOAD_FAIL, new ApiCode(PIC_UPLOAD_FAIL, "图片上传失败"));
    }

    public static ApiCode findCode(int code) {
        ApiCode model = map.get(code);
        ApiCode apiCode = ApiCode.build();
        apiCode.setCode(model.getCode());
        apiCode.setMsg(model.getMsg());
        apiCode.setData(null);
        return apiCode;
    }

    public static ApiCode findCode(int code, Object data) {
        ApiCode model = map.get(code);
        ApiCode apiCode = ApiCode.build();
        apiCode.setCode(model.getCode());
        apiCode.setMsg(model.getMsg());
        apiCode.setData(data);
        return apiCode;
    }

    private int code;
    private String msg;
    private Object data;

    ApiCode() {
    }

    public static ApiCode build() {
        return new ApiCode();
    }

    public static ApiCode build(Object data) {
        return build(SUCCESS, data);
    }

    public static ApiCode build(int code, Object data) {
        return new ApiCode(code, data);
    }

    public static ApiCode build(int code, String msg) {
        return new ApiCode(code, msg);
    }

    public static ApiCode build(int code, String msg, Object data) {
        return new ApiCode(code, msg, data);
    }

    ApiCode(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    ApiCode(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    ApiCode(int code, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}
